﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
using Benefit.Models.Year;
namespace Benefit.Service.Year
{
    public class ServiceAccountYearMoney : Benefit.Interface.Year.IAccountYearMoney
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountYearMoney(Benefit.DB.DBManager db)
        {
            this.db = db;
        }



        /// <summary>
        /// 删除当年的记录
        /// </summary>
        /// <param name="month"></param>
        /// <param name="year"></param>
        public void Delete(int year)
        {
            var query = from t in db.AccountYearMoney where t.Year == year select t;
            foreach (Models.Year.AccountYearMoney c in query)
            {
                db.AccountYearMoney.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 初始化账户年资金信息
        /// </summary>
        /// <param name="month"></param>
        /// <param name="year"></param>
        public void InitAccountYearMoney(int year)
        {
            var query = db.Accounts.ToList();
            foreach (Models.Sys.Account account in query)
            {
                Models.Year.AccountYearMoney change = new Models.Year.AccountYearMoney();
                Models.Month.AccountMonthMoney lastMonthMoney = new Models.Month.AccountMonthMoney();
                Models.Month.AccountMonthMoney fristMonthMoney = new Models.Month.AccountMonthMoney();
                change.AccountId = account.Id;
                var _query = (from t in db.AccountMonthMoney where t.AccountId == account.Id && t.Year == year orderby t.Id descending select t).ToList();
                if (_query.Count() > 0)
                {
                    lastMonthMoney = _query.First();
                    fristMonthMoney = _query.LastOrDefault();
                }
                var existQuery = from t in db.AccountYearMoney where t.Year == year && t.AccountId == account.Id select t;
                if (existQuery.Count() > 0)
                {
                    change = existQuery.First();
                }

                change.AppendMargin = lastMonthMoney.AppendMargin;
                change.AvailableMoney = lastMonthMoney.AvailableMoney;
                change.BaseMargin = lastMonthMoney.BaseMargin;
                change.CloseProfit = _query.Sum(a => a.CloseProfit);
                change.Commission = _query.Sum(a => a.Commission);
                change.DeliveryCommission = _query.Sum(a => a.DeliveryCommission);
                change.DeliveryMargin = lastMonthMoney.DeliveryMargin;
                change.Deposit = _query.Sum(a => a.Deposit);
                change.DynamicMoney = lastMonthMoney.DynamicMoney;
                change.FrozenCommission = lastMonthMoney.FrozenCommission;
                change.FrozenMargin = lastMonthMoney.FrozenMargin;
                change.LongPosition = lastMonthMoney.LongPosition;
                change.Margin = lastMonthMoney.Margin;
                change.Mortgage = lastMonthMoney.Mortgage;
                change.PositionProfit = lastMonthMoney.PositionProfit;
                change.Premortgage = fristMonthMoney.Premortgage;
                change.PresetTlePosit = fristMonthMoney.PresetTlePosit;
                change.RiskRate = lastMonthMoney.RiskRate;
                change.SettlePosit = lastMonthMoney.SettlePosit;
                change.ShortPosition = lastMonthMoney.ShortPosition;
                change.WithDraw = _query.Sum(a => a.WithDraw);
                change.WithDrawQuota = lastMonthMoney.WithDrawQuota;
                change.Year = year;
                if (existQuery.Count() == 0)
                {
                    db.AccountYearMoney.Add(change);
                }
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 获取账号和交易员年资金信息
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public List<AccountYearMoney> GetAccountAndAllChange(int operatorid, int year)
        {
            List<AccountYearMoney> list = new List<AccountYearMoney>();
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
            Models.Sys.TradeHistory th = ith.GetYearLastTradingDay(year);
            List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetDateTimeFromTradeHistoryId(th.Id), operatorid);
            foreach (Models.Sys.Account account in accounts)
            {
                var query = db.AccountYearMoney.Where(a => a.Year == year).Where(a => a.AccountId == account.Id);
                if (query.Count() > 0)
                {
                    list.Add(query.First());
                }
            }
            return list;
        }
    }
}